package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.TextUtils;


import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.util.Format2Util;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.DailyCoordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DailyLayer extends BasicLayer
{
	protected final double m_dbNone = Double.MIN_VALUE;

	private int m_StartIndex;

	public DailyLayer()
	{
		super();
	}

	@Override
	public Layer.ChartTypeEnum getLayerTypeEnum()
	{
		return Layer.ChartTypeEnum.DAILY;
	}
	
	@Override
	public Coordinate createCoordinate()
	{
		return new DailyCoordinate();
	}

	public List<CandleData> convertDate(List<CandleData> pList)
	{
		if (pList == null)
		{
			return null;
		}
		
		int tListSize = pList.size();
		List<Integer> tSession = ((DailyCoordinate)m_callback.getCoordinate()).getSession();
		
		List<CandleData> tList = new ArrayList<CandleData>();
		
		int tTempIndex = -1;

		for (int ii = 0; ii < tListSize; ii++)
		{
			long tTime = pList.get(ii).m_nDate;
			int tIndex = tSession.indexOf(tTime);
			if (tIndex > tTempIndex)
			{
				if (tTempIndex == -1)
				{
					m_StartIndex = tIndex;
				}

				for (int jj = tTempIndex + 1; jj < tIndex; jj++)
				{
					tList.add(new CandleData(0, tSession.get(jj), pList.get(ii).getDbClose(), 0,pList.get(ii).m_dbAvgPx, 0));
				}
				tTempIndex = tIndex;

				tList.add(pList.get(ii).clone());
			}
		}
		
		if (tList.size() > 0)
		{
			tList.get(tList.size() - 1).m_bDrawDot = true;
		}
		
		return tList;
	}

	@Override
	public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		//获取坐标系
		Coordinate tCoordinate = m_callback.getCoordinate();
		List<CandleData> tDataList = convertDate(m_callback.getCandleDataList());

		//定义初始化文本
		String tCloseText = null;
		String tOpenText = null;
		String tHighText = null;
		String tLowText = null;
		String tTimeText = null;
		String tValueText = null;
		float tVolumnText = 0;
		String tAvgText = null;
		int tXPoint = pPoint.x;
		if (tDataList != null && tDataList.size() > 0)
		{
			int tListSize = tDataList.size();
			int tStartIndex = m_StartIndex; // tCoordinate.getStartIndex();
			float tXFirstPosition = tCoordinate.getXFirstPosition();
			float tXInterval = tCoordinate.getXInterval();
			float tYFirstposition = tCoordinate.getYFirstPosition();
			float tYMaxValue = tCoordinate.getMaxValue();
			float tYMinValue = tCoordinate.getMinValue();
			float tYValueDist = tYMaxValue - tYMinValue;
			float tYValueLength = tCoordinate.getYInterval() * tCoordinate.getYIntervalCount();
			RectF tChartRect = tCoordinate.getLayerRectF();

			float tCurrentYPosition = 0.f;
			float tTempYPosition = 0.f;
			float tCurrentXPosition = 0.f;
			float tTempXPosition = 0.f;


			int tDotColor = R.color.sys_even;
			float tLineWidth = 2.f;
			Path tAreaPath = new Path();

			pPaint.setStrokeWidth(2);
			pPaint.setStyle(Paint.Style.FILL);
			pPaint.setAntiAlias(true);
			pPaint.setColor(m_callback.getSysBgColor());
			float tTopPosition = tCoordinate.getYFirstPosition();

			drawAvgLine(pCanvas, pPaint, tCoordinate, tYFirstposition, tYMinValue,tYMaxValue, tYValueDist, tYValueLength);

			for (int i = tStartIndex; i < tListSize; i++)
			{
				//如果收盘价为空就跳过
				if (tDataList.get(i).getDbClose() == m_dbNone)
				{
					continue;
				}

				float tVolumn = (float) tDataList.get(i).getDbClose();

				tTempXPosition = tCurrentXPosition;
				tCurrentXPosition = tXFirstPosition + tXInterval * i;
				tTempYPosition = tCurrentYPosition;
				tCurrentYPosition = tYFirstposition - ((tVolumn - tYMinValue) * tYValueLength / tYValueDist);

				tTopPosition = tTopPosition < tCurrentYPosition ? tTopPosition : tCurrentYPosition;


				if (((tCurrentXPosition - tXInterval / 2) <= pPoint.x && pPoint.x < (tCurrentXPosition + tXInterval / 2)) || ((i == tListSize - 1) && pPoint.x > tCurrentXPosition + tXInterval / 2) || (i == tStartIndex && pPoint.x < tCurrentXPosition - tXInterval / 2))
				{
					float tValueaa = (tYFirstposition - pPoint.y) * tYValueDist / tYValueLength + tYMinValue;
					tValueText = FormatUtil.getFormatString(tValueaa, m_callback.getDigit());

					ChartData.ChartPeroid tChartType = m_callback.getChartType();

					tVolumnText = tDataList.get(i).m_lVolume;
					tCloseText = String.valueOf((float) tDataList.get(i).getDbClose());
					tOpenText = String.valueOf((float) tDataList.get(i).getDbOpen());
					tHighText = String.valueOf((float) tDataList.get(i).getDbHigh());
					tLowText = String.valueOf((float) tDataList.get(i).getDbLow());
					tAvgText = String.valueOf((float) tDataList.get(i).m_dbAvgPx);

					tTimeText = getDisplayX(tChartType, tDataList.get(i));
					tXPoint = (int) tCurrentXPosition;
				}

				if (i != tStartIndex)
				{
					pCanvas.drawLine(tTempXPosition, tTempYPosition, tCurrentXPosition, tCurrentYPosition, pPaint);
				}
//				pCanvas.drawCircle(tCurrentXPosition, tCurrentYPosition, 3, pPaint);

				if (i == tStartIndex)
				{
					tAreaPath.moveTo(tCurrentXPosition, tChartRect.bottom);
				}
				
				tAreaPath.lineTo(tCurrentXPosition, tCurrentYPosition);
				if (i == (tListSize - 1))
				{
					tAreaPath.lineTo(tCurrentXPosition, tChartRect.bottom);
					tAreaPath.lineTo(tXFirstPosition + m_StartIndex * tXInterval, tChartRect.bottom);
				}

				if (tDataList.get(i).m_bDrawDot)
				{
					//获取涨跌颜色
//					if (tCurrentYPosition > tTempYPosition)
//					{
//						tDotColor = ResourceManager.getColor(FDTApplication.getApp(), "sys_down");
//					}
//					else if (tCurrentYPosition < tTempXPosition)
//					{
//						tDotColor = ResourceManager.getColor(FDTApplication.getApp(), "sys_up");
//					}
//					else
					{
						tDotColor = R.color.sys_even;//ResourceManager.getColor(FDTApplication.getApp(), "sys_even");
					}
					pPaint.setColor(tDotColor);
					pCanvas.drawCircle(tCurrentXPosition, tCurrentYPosition, tLineWidth * 2, pPaint);
				}
			}

//			if (m_bGradient)
//			{
			Shader mShader = new LinearGradient(0, tChartRect.bottom, 0, tTopPosition,
					Color.argb((Color.alpha(m_callback.getSysBgColor())) / 3, Color.red(m_callback.getSysBgColor()), Color.green(m_callback.getSysBgColor()), Color.blue(m_callback.getSysBgColor())), Color.argb((Color.alpha(m_callback.getSysBgColor())) / 15, Color.red(m_callback.getSysBgColor()), Color.green(m_callback.getSysBgColor()), Color.blue(m_callback.getSysBgColor())), Shader.TileMode.CLAMP);
			pPaint.setShader(mShader);
//			}
//			else
//			{
//				pPaint.setColor(Color.argb((Color.alpha(m_callback.getSysBgColor())) / 5, Color.red(m_callback.getSysBgColor()), Color.green(m_callback.getSysBgColor()), Color.blue(m_callback.getSysBgColor())));
//			}

			pCanvas.drawPath(tAreaPath, pPaint);
			pPaint.reset();
			pPaint.setAntiAlias(true);
		}

		RectF tLayerRect = tCoordinate.getLayerRectF();

		setCrossData(pCanvas, pPaint, tTimeText, tValueText, new Point(tXPoint, pPoint.y));
		if (tLayerRect.contains(tXPoint, pPoint.y))
		{


			if (m_callback != null)
			{
				int tDigit = Math.max(tCoordinate.getDigit(), 1);

				tCloseText = getDigitString(tCloseText, tDigit);
				tOpenText = getDigitString(tOpenText, tDigit);
				tHighText = getDigitString(tHighText, tDigit);
				tLowText = getDigitString(tLowText, tDigit);
				tAvgText = getDigitString(tAvgText, tDigit);

				HashMap<String, String> tMap = new HashMap<String, String>();
				tMap.put("Close", tCloseText);
				tMap.put("Open", tOpenText);
				tMap.put("High", tHighText);
				tMap.put("Low", tLowText);
				tMap.put("Volumn", tVolumnText == 0 ? "" : Format2Util.doubleToHumanReadableFormat(tVolumnText, 2));
				tMap.put("Avg",tAvgText);
				tMap.put("Time",tTimeText);
				m_callback.pointFeedback(tMap,pPoint );
			}
		}
	}

	private String getDigitString(String value, int digit)
	{
		if (! TextUtils.isEmpty(value))
		{
			try
			{
				value = FormatUtil.getFormatString(Double.parseDouble(value), digit);
			}
			catch (Exception e)
			{
			}
		}
		else
		{
			value = "";
		}

		return value;
	}

	private float getScale(float start, float end, float middle)
	{
		return (start - middle) / (start - end);
	}

	@Override
	public boolean isDrawAvgText() {
		return true;
	}
}
